//企微群推送机器人
import { useState, useEffect } from 'react';
import { Form, Input, message } from 'antd';
import CommonModal from '@/components/CommonModal';
import CommonLoading from '@/components/CommonLoading';
import { updateWechatUserId, batchUpdateWechatUserId } from '../service';
import { isEmpty } from 'lodash';

const layout = {
  //查询表单排布
  labelCol: {
    span: 8,
  },
  wrapperCol: {
    span: 16,
  },
};

const { TextArea } = Input;

const PushRobot = ({ visible, item, selectedRow, handleOk, handleCancel }) => {
  const [form] = Form.useForm();
  const [loading, setLoading] = useState(false);

  console.log('selectedRow1234', selectedRow);

  useEffect(() => {
    if (visible) {
      form.resetFields();
    }
  }, [visible]);

  const onOk = () => {
    form.validateFields().then(async (val) => {
      const { wechatUserId } = val;
      const wechatUserIdValue = wechatUserId.replace(/\n$/, '');
      setLoading(true);
      let res,
        params = {};
      if (!isEmpty(item)) {
        //单个
        params = {
          storeId: item.storeId,
          wechatUserId: wechatUserIdValue,
        };
        res = await updateWechatUserId(params);
      } else {
        params = {
          storeIds: selectedRow.map((item) => item.storeId),
          wechatUserId: wechatUserIdValue,
        };
        res = await batchUpdateWechatUserId(params);
      }
      const { success, msg } = res;
      setLoading(false);
      if (success) {
        message.success(msg);
        handleOk();
      }
    });
  };

  return (
    <CommonModal
      visible={visible}
      title="企业微信接收人"
      width="480px"
      //   minHeight="140px"
      onOk={onOk}
      onCancel={handleCancel}
    >
      <Form
        initialValues={{
          wechatUserId: item?.wechatUserId,
        }}
        form={form}
        // {...layout}
        name="set-robot-modal"
      >
        <Form.Item
          //   rules={[{ required: true }]}
          label="企业微信推送消息接收人ID"
          name="wechatUserId"
        >
          <TextArea
            placeholder="请输入自定义地址，最多50个字符"
            maxLength={50}
            showCount
            // autoFocus
            // autoSize={{ minRows: 4, maxRows: 4 }}
            style={{ height: 150 }}
          />
        </Form.Item>
      </Form>

      <CommonLoading loading={loading} />
    </CommonModal>
  );
};

export default PushRobot;
